"use strict";

angular.module('app.sensor').controller('SensorChartsCtrl', function ($scope,$stateParams,$http,$cookieStore) {
    // $scope.userid = $cookieStore.get('userid');
    var device = {};
    var chart = [];//原始曲线数据
    if ($stateParams.deviceid != null && $stateParams.deviceid != '') {
        device.deviceid =  $stateParams.deviceid;
        document.getElementById("deviceid_s").value=device.deviceid;
        $http.get(baseurl+"/history_data?deviceid="+device.deviceid).then(function(res){           
            if(res.data.ret == 1){
                chart = res.data.info;
                chart_handle(chart);
            }
        })
    }
    // $http.get(baseurl+"/history_data_yk").then(function(res){
    //      console.log(res.data.info[0]);
    // });
    var temp = [], temp_time = [];
    var press = [], press_time = [];
    $scope.sends = function () {

        device.deviceid = $("#deviceid_s").val();
        var timefrom = $("#time_s").val();
        var timeto = $("#time_e").val();
        if(timefrom == "" && timeto == ""){
            device.time = "";
        }else{
            device.time = $("#time_s").val()+","+$("#time_e").val();
        }
        // console.log(device);
        $http.get(baseurl+"/history_data?deviceid="+device.deviceid+"&time="+device.time).then(function(res){
            // console.log(res.data);
            if(res.data.ret == 1){
                chart = res.data.info;
                chart_handle(chart.slice(chart.length-10, chart.length));
            }
        })
    }
    $scope.sends();
    function chart_handle(items){    
         var i = 0;
         temp_time = items.map(function (item) {

             i++;
             return [i, moment(item.time).format("YYYY-MM-DD HH:mm:ss")];
         });

         i = 0;

         temp = items.map(function (item) {
            // console.log(item);
            i++;
            return [i, parseFloat(item.temp).toFixed(1)];
        });
        
         i = 0;
         press_time = items.map(function (item) {

             i++;
             return [i, moment(item.time).format("YYYY-MM-DD HH:mm:ss")];
         });

         i = 0;

         press = items.map(function (item) {

             i++;
             return [i, parseFloat(item.press).toFixed(1)];
         });
        pagefunction();
    }

    var plot_1, plot_2, plot_3, plot_4, plot_5, plot_6, plot_7, plot_8;

    // PAGE RELATED SCRIPTS

    var pagefunction = function() {

        /* chart colors default */
        var $chrt_border_color = "#efefef";
        var $chrt_grid_color = "#DDD"
        var $chrt_main = "#E24913";			/* red       */
        var $chrt_second = "#6595b4";		/* blue      */
        var $chrt_third = "#FF9F01";		/* orange    */
        var $chrt_fourth = "#7e9d3a";		/* green     */
        var $chrt_fifth = "#BD362F";		/* dark red  */
        var $chrt_mono = "#000";

        
        /* Sin chart temp */

        if ($("#sin-chart-t").length) {
            
        
            plot_2 = $.plot($("#sin-chart-t"), [{
                data : temp,
                label : "温度"
            }], {
                series : {
                    lines : {
                        show : true
                    },
                    points : {
                        show : true
                    }
                },
                grid : {
                    hoverable : true,
                    clickable : true,
                    tickColor : $chrt_border_color,
                    borderWidth : 0,
                    borderColor : $chrt_border_color,
                },
                tooltip : true,
                tooltipOpts : {
                    //content : "Value <b>$x</b> Value <span>$y</span>",
                    defaultTheme : false
                },
                colors : [$chrt_second, $chrt_fourth],
                yaxis : {
                    min : 5.1,
                    max : 100.1
                },
                xaxis : {
                    ticks:temp_time
                }
            });

            $("#sin-chart-t").bind("plothover", function (event, pos, item) {
                if (item) {
                    // console.log(item)
                    $("#flotTip").text(item.series.label + "值为 "+item.datapoint[1].toFixed(2)+"°C");
                    // console.log($('#flotTip').html())
                }
            });
        }

        /* end sin chart */


        /* Sin chart press */

        if ($("#sin-chart-p").length) {
        
            plot_2 = $.plot($("#sin-chart-p"), [{
                data: press,
                label: "压力"
            }], {
                series: {
                    lines: {
                        show: true
                    },
                    points: {
                        show: true
                    }
                },
                grid: {
                    hoverable: true,
                    clickable: true,
                    tickColor: $chrt_border_color,
                    borderWidth: 0,
                    borderColor: $chrt_border_color,
                },
                tooltip: true,
                tooltipOpts: {
                    //content : "Value <b>$x</b> Value <span>$y</span>",
                    defaultTheme: false
                },
                colors: [$chrt_second, $chrt_fourth],
                yaxis: {
                    min: 100,
                    max: 50000
                },
                xaxis: {
                    ticks: press_time
                }
            });

            $("#sin-chart-p").bind("plothover", function (event, pos, item) {
                if (item) {
                    $("#flotTip").text(item.series.label + "值为 "+item.datapoint[1].toFixed(2)+"Pa");
                }
            });
        }

        /* end sin chart */
    
        // START AND FINISH DATE
        $('#time_s').datepicker({
                //dateFormat : 'dd.mm.yy',
                dateFormat: 'yy-mm-dd',
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>',
                monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],  
                dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  
                dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],  
                dayNamesMin: ['日','一','二','三','四','五','六'],
                onSelect: function (selectedDate) {
                    $('#time_e').datepicker('option', 'minDate', selectedDate);
                }
            });

            $('#time_e').datepicker({
                dateFormat: 'yy-mm-dd',
                prevText: '<i class="fa fa-chevron-left"></i>',
                nextText: '<i class="fa fa-chevron-right"></i>',
                monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],  
                dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],  
                dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],  
                dayNamesMin: ['日','一','二','三','四','五','六'],
                onSelect: function (selectedDate) {
                    $('#time_s').datepicker('option', 'maxDate', selectedDate);
                }
            });


    };


    var pagedestroy = function(){

        //destroy flots
        plot_1.shutdown();
        plot_1 = null;

        plot_2.shutdown();
        plot_2 = null;

        plot_3.shutdown();
        plot_3 = null;

        plot_4.shutdown();
        plot_4 = null;

        plot_5.shutdown();
        plot_5 = null;

        plot_6.shutdown();
        plot_6 = null;

        plot_7.shutdown();
        plot_7 = null;

        plot_8.shutdown();
        plot_8 = null;

        if (debugState){
            root.console.log("✔ Flots destroyed");
        }

    }

    pagefunction();
 

});
